package org.idea.spring.framework.http.starter;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Author linhao
 * @Date created in 6:21 下午 2021/4/30
 */
@Configuration
//需要先加载HttpProperties类才可以
@EnableConfigurationProperties(HttpProperties.class)
public class HttpAutoConfiguration {

    @Resource
    private HttpProperties httpProperties;

    @Bean
    //在该bean不存在的情况下此方法才会执行
    @ConditionalOnMissingBean
    //在该bean存在的时候才会触发创建 @link {https://docs.spring.io/spring-boot/docs/2.0.0.M5/reference/htmlsingle/#boot-features-condition-annotations}
//    @ConditionalOnBean
    public SpringHttpClient init(){
        SpringHttpClient springHttpClient = new SpringHttpClient();
        springHttpClient.setUrl(httpProperties.getUrl());
        springHttpClient.setCharsetName(httpProperties.getCharsetName());
        return springHttpClient;
    }
}
